name: MindsDB Tests & Deploy

on:
  pull_request:
    branches: [stable, staging]
    paths-ignore:
      - 'docs/**'
      - '**/*.md'
jobs:
  code_checking:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3.5.3
      - name: Set up Python 3.8
        uses: actions/setup-python@v4.7.0
        with:
          python-version: 3.9
      - name: Run code checks
        shell: bash
        run: |
          python tests/scripts/check_code.py
      - name: Get changed files
        id: changed-files
        uses: tj-actions/changed-files@v41
      - name: Run pre-commit
        uses: pre-commit/action@v3.0.0
        with:
          extra_args: --files ${{ steps.changed-files.outputs.all_changed_files }}
  matrix_prep:
    needs: [code_checking]
    runs-on: ubuntu-latest
    outputs:
      matrix: ${{ steps.set-matrix.outputs.matrix }}
    steps:
      - uses: actions/checkout@v3.5.3
      - id: set-matrix
        uses: JoshuaTheMiller/conditional-build-matrix@v1.0.1
        with:
          filter: '[?runOnBranch==`${{ github.ref }}` || runOnBranch==`always`]'
  test:
    needs: matrix_prep
    strategy:
      matrix: ${{fromJson(needs.matrix_prep.outputs.matrix)}}
    name: Tests
    runs-on: ${{ matrix.runs_on }}
    if: github.ref_type == 'branch'
    steps:
    - uses: actions/checkout@v3.5.3
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v4.7.0
      with:
        python-version: ${{ matrix.python-version }}-dev
    - name: Install dependencies
      run: |
        pip install --no-cache-dir .
        pip install -r requirements/requirements-test.txt
        pip install .[lightwood]  # TODO: for now some tests rely on lightwood
        pip freeze
      shell: bash
    - name: Set up environment
      shell: bash
      run: |
        mkdir prometheus_metrics
    - name: Run integration api and flow tests
      run: |

        if [ "$RUNNER_OS" == "Linux" ]; then
          # Unit tests
          # echo -e "\n===============\nUnit tests\n===============\n"

          env PYTHONPATH=./ pytest tests/unit/test_executor.py

          env PYTHONPATH=./ pytest tests/unit/test_project_structure.py

          env PYTHONPATH=./ pytest tests/unit/test_predictor_params.py

          env PYTHONPATH=./ pytest tests/unit/test_mongodb_handler.py

          env PYTHONPATH=./ pytest tests/unit/test_mongodb_server.py

          env PYTHONPATH=./ pytest tests/unit/test_cache.py
        
          env PYTHONPATH=./ pytest tests/unit/test_llm_utils.py

          pytest -vx tests/integration_tests/flows/test_ml_task_queue.py

          # MySQL API
          echo -e "\n===============test MySQL API===============\n"
          pytest -vx tests/integration_tests/flows/test_mysql_api.py
          #
          # MySQL binary API
          echo -e "\n===============test MySQL binary API===============\n"
          pytest -vx -k 'not TestMySqlApi' tests/integration_tests/flows/test_mysql_bin_api.py

          # echo -e "\n===============TS predictions===============\n"
          pytest -svx tests/integration_tests/flows/test_ts_predictions.py

          # HTTP
          echo -e "\n===============test HTTP===============\n"
          pytest -vx tests/integration_tests/flows/test_http.py

          # Company independent
          echo -e "\n===============test company independent===============\n"
          pytest -vx tests/integration_tests/flows/test_company_independent.py
        fi
      shell: bash
      env:
        PROMETHEUS_MULTIPROC_DIR: ./prometheus_metrics
        CHECK_FOR_UPDATES: False
        AWS_SECRET_ACCESS_KEY: ${{secrets.AWS_SECRET_ACCESS_KEY}}
        AWS_ACCESS_KEY_ID: ${{secrets.AWS_ACCESS_KEY_ID}}
    - name: Check if actor is an organization member
      id: check-membership
      run: |
        if [[ "${{ github.actor }}" == "mindsdb/"* ]]; then
          echo "::set-output name=isOrgMember::true"
        else
          echo "::set-output name=isOrgMember::false"
        fi
      shell: bash
    - name: Run ML Engines tests
      if: steps.check-membership.outputs.isOrgMember == 'true'
      run: |
        if [ "$RUNNER_OS" == "Linux" ]; then
          # First-tier ML engines
          echo -e "\n===============test ML engines===============\n"
          # pytest -vx tests/unit/ml_handlers/test_openai.py
          # pytest -vx tests/unit/ml_handlers/test_timegpt.py  # Note: timegpt doesn't work, fix please
          # pytest -vx tests/unit/ml_handlers/test_anyscale_llm.py

          # pending rework
          # pytest -vx tests/unit/ml_handlers/test_statsforecast.py
          # pytest -vx tests/unit/ml_handlers/test_huggingface.py
          # pytest -vx tests/unit/ml_handlers/test_lightwood.py
      shell: bash
      env:
        CHECK_FOR_UPDATES: False
        OPENAI_API_KEY: ${{secrets.OPENAI_API_KEY}}
        ANYSCALE_ENDPOINTS_API_KEY: ${{secrets.ANYSCALE_ENDPOINTS_API_KEY}}
        PROMETHEUS_MULTIPROC_DIR: ./prometheus_metrics
    - name: Run Handlers tests and submit Coverage to coveralls
      run: |
        pytest --cov=tests/unit/handler tests/unit/handlers/test_postgresql.py && coveralls --service=github
      env:
        COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }}
        PROMETHEUS_MULTIPROC_DIR: ./prometheus_metrics
        github_token: ${{ secrets.REPO_DISPATCH_PAT_TOKEN }}
